package com.artxis.cms.commands {
	
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.artxis.cms.business.LoginDelegate;
	import com.artxis.cms.events.LoginEvent;
	import com.artxis.cms.events.MenuAdminEvent;
	import com.artxis.cms.model.ViewModelLocator;
	import com.artxis.cms.vo.AccountVO;
	import com.artxis.cms.vo.MenuAdminVO;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	
	public class LoginCommand implements ICommand, IResponder {
	
		public var modelLocator:ViewModelLocator = ViewModelLocator.getInstance();
		
		public function LoginCommand() {
			
		}
			[Bindable] 
            public var menuUser:Array = [
                {label: "Profiles", data: "Profiles"},
                {label: "User Pages", data: "calendar"}
            ];
            [Bindable] 
            public var menuAdmin:Array = [
                {label: "Profiles", data: "Profiles"},
                {label: "Admin Pages", data: "adminPanel"}
            ];
		public function execute(event:CairngormEvent):void {
			
			var loginEvent:LoginEvent = event as LoginEvent;
			
			var delegate:LoginDelegate = new LoginDelegate( this );
			delegate.login( loginEvent.loginAttempt );
			
		}
		
		public function result( event:Object ):void {
			modelLocator.userAccountInfo = AccountVO( event.result );
			if(modelLocator.userAccountInfo.accountid >0) {
			//if(event.result == true) {	
				modelLocator.workflowState = ViewModelLocator.WELCOME_SCREEN;
 				if(modelLocator.userAccountInfo.type=="admin"){
 					
					modelLocator.menuArr = menuAdmin;
					modelLocator.isAdmin = true;
			    //get admin menu
				var eventMenuAdmin:MenuAdminEvent = new  MenuAdminEvent( new  MenuAdminVO() );
				eventMenuAdmin.dispatch();
				}else{
					modelLocator.menuArr = menuUser;
					modelLocator.isAdmin = false;	
				}
				modelLocator.butonLoginLabel = ViewModelLocator.BUTTON_CONROL_PANEL;
			} else {
			
				mx.controls.Alert.show("Incorrect Login Information","Error");
				
			}
			
		}
		
		public function fault( event:Object ):void {
			Alert.show(String(event.fault.faultDetail));
			trace("CONNECTION ERROR");
			
		}
		
	}
}